package tmc.app.data;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import tmc.app.luckydraw.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.climaxi.core.data.JSONParser;
import com.climaxi.core.helper.NetWork;
import com.climaxi.core.ui.Loading;

public class ProcessData {
	
	public static void ringPhone(Activity mActivity) {
		Vibrator vibrator;
		vibrator = (Vibrator) mActivity
				.getSystemService(Context.VIBRATOR_SERVICE);
		vibrator.vibrate(100);
	}
	
	public static boolean isInternet(Context m) {
		return NetWork.isConnected(m);
	}
	
	public static void showDialogThongbao(final Context mContext, String txt) {
		final Dialog dialog = new Dialog(mContext, android.R.style.Theme_Black);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.layout_dialog_notification);
		dialog.getWindow().setBackgroundDrawableResource(
				R.drawable.bg_transparent);
		Button btnOpt1 = (Button) dialog.findViewById(R.id.btn_yes);
		TextView txtName = (TextView) dialog.findViewById(R.id.txt_name);
		txtName.setText(txt);
		btnOpt1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
			ringPhone((Activity)mContext);
				dialog.dismiss();
			}
		});

		dialog.setCancelable(false);
		dialog.show();

	}
	
	public static void showDialogLoading(Dialog mDialog, String msg) {

		mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialog.setContentView(R.layout.layout_loading);

		Loading loader = (Loading) mDialog.findViewById(R.id.loader);
		loader.start();

		TextView tvContent = (TextView) mDialog.findViewById(R.id.tv_loader);
		if (!msg.equals(""))
			tvContent.setText(msg);

		mDialog.setCancelable(false);
		mDialog.show();
	}

	public static JSONObject postRecordData(int id_giai_thuong,ObjRecordData recordData) {
		JSONParser jsonParser = new JSONParser();
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		switch (id_giai_thuong) {
		case 1:
			nameValuePairs.add(new BasicNameValuePair("id_giai_thuong", "1"));
			nameValuePairs.add(new BasicNameValuePair("so_luong_set", recordData.getDataAo()));
			nameValuePairs.add(new BasicNameValuePair("so_luong_trung", recordData.getResultAo()));
			nameValuePairs.add(new BasicNameValuePair("ngay_set", recordData.getDate()));
			break;
		case 2:
			nameValuePairs.add(new BasicNameValuePair("id_giai_thuong", "2"));
			nameValuePairs.add(new BasicNameValuePair("so_luong_set", recordData.getDataBalo()));
			nameValuePairs.add(new BasicNameValuePair("so_luong_trung", recordData.getResultBalo()));
			nameValuePairs.add(new BasicNameValuePair("ngay_set", recordData.getDate()));
			break;
		case 3:
			nameValuePairs.add(new BasicNameValuePair("id_giai_thuong", "3"));
			nameValuePairs.add(new BasicNameValuePair("so_luong_set", recordData.getDataSotay()));
			nameValuePairs.add(new BasicNameValuePair("so_luong_trung", recordData.getResultSotay()));
			nameValuePairs.add(new BasicNameValuePair("ngay_set", recordData.getDate()));
			break;
		case 4:
			nameValuePairs.add(new BasicNameValuePair("id_giai_thuong", "4"));
			nameValuePairs.add(new BasicNameValuePair("so_luong_set", recordData.getDataBut()));
			nameValuePairs.add(new BasicNameValuePair("so_luong_trung", recordData.getResultBut()));
			nameValuePairs.add(new BasicNameValuePair("ngay_set", recordData.getDate()));
			break;
		case 5:
			nameValuePairs.add(new BasicNameValuePair("id_giai_thuong", "5"));
			nameValuePairs.add(new BasicNameValuePair("so_luong_set", recordData.getDataMockhoa()));
			nameValuePairs.add(new BasicNameValuePair("so_luong_trung", recordData.getResultMockhoa()));
			nameValuePairs.add(new BasicNameValuePair("ngay_set", recordData.getDate()));
			break;
		default:
			break;
		}
		
		JSONObject data = jsonParser.postDataObject(GlobalVariables.LINK_POST_RECORD_DATA,
				nameValuePairs);
		return data;
	}
	
	public static JSONObject postRecordUser(ObjUser recordUser) {
		JSONParser jsonParser = new JSONParser();
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("ten_quan", recordUser.getUsername()));
		nameValuePairs.add(new BasicNameValuePair("so_dt", recordUser.getPhone()));
		nameValuePairs.add(new BasicNameValuePair("so_luot_quay", recordUser.getCount()));
		nameValuePairs.add(new BasicNameValuePair("is_win", recordUser.getIsWin()));
		nameValuePairs.add(new BasicNameValuePair("id_win", recordUser.getGiaithuong()));
		nameValuePairs.add(new BasicNameValuePair("ngay_quay", recordUser.getDate()));
		nameValuePairs.add(new BasicNameValuePair("ten", recordUser.getTen()));
		
		JSONObject data = jsonParser.postDataObject(GlobalVariables.LINK_POST_RECORD_USER,
				nameValuePairs);
		return data;
	}
}
